<?php

namespace app\models;

use think\Model;

/**
 * Class ClassTimeModel
 * @package app\models
 */
class ClassTimeModel extends Model
{
    protected $table = 'edu_class_time';

    /**
     * @param $time_slot_id
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getById($time_slot_id)
    {
        if (!$time_slot_id) {
            return [];
        }

        return $this->where(['id' => $time_slot_id])
            ->field('id,start_time,end_time,time_long')
            ->find();
    }

    /**
     * 获取上课列表.
     *
     * @param $param
     *
     * @throws \think\Exception
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getClassTimeLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['course_name']) && '' !== $param['course_name']) {
            $where['course_name'] = ['like', $param['course_name'].'%'];
        }
        if (isset($param['type']) && '' !== $param['type']) {
            if($param['type'] == 'all'){
                $where['type'] = ['in','1,0'];
            }else if($param['type'] == 'zm'){
                $where['type'] = 0;
            }else if($param['type'] == 'gzr'){
                $where['type'] = 1;
            }
        }

		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}


        $listData = $this->where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['campus_name'] = CampusModel::get(['id' => $item['campus_id']])['campus_name'];
                $item['start_time'] = date('H:i',strtotime($item['start_time']));
                $item['end_time'] = date('H:i',strtotime($item['end_time']));
            }
        }
        return $listData;
    }
}
